package fr.emse.tatiana.corpus;

import java.util.Collection;
import java.util.List;
import java.util.Vector;

import org.eclipse.core.resources.*;
import org.eclipse.core.runtime.CoreException;
import fr.emse.tatiana.TatianaConstants;
import fr.emse.tatiana.corpus.CorpusFile;

public class Corpus {
	
	private Vector<CorpusFile> xmlfiles = new Vector<CorpusFile>();
	private Vector<IFile> replayables = new Vector<IFile>();
	private Vector<IFile> analyses = new Vector<IFile>();
	private Vector<CorpusFile> videofiles = new Vector<CorpusFile>();
	private IProject project;
	private long extractstart;
	private long extractend;

	// reads a tatiana corpus into memory
	
	public Corpus(IProject project) {
		this.project = project;
		try {
			IResource[] files = project.getFolder(TatianaConstants.TRACES_FOLDER_NAME).members();
			// all the corpus files must be in the traces subfolder
			for (int i=0; i<files.length; i++) {
				// assume that there are no sub-folders
				if (files[i].getType() != IResource.FILE) 
					continue;
				IFile file = (IFile) files[i];
				CorpusFile cf = new CorpusFile(file);
				if (cf.getType() == CorpusFile.UNKNOWN)
					continue;
				this.addFile(cf);
			}
			files = project.getFolder(TatianaConstants.REPLAYABLES_FOLDER_NAME).members();
			// all the corpus files must be in the traces subfolder
			for (int i=0; i<files.length; i++) {
				// assume that there are no sub-folders
				if (files[i].getType() != IResource.FILE) 
					continue;
				IFile file = (IFile) files[i];
				replayables.add(file);
			}
			files = project.getFolder(TatianaConstants.ANALYSES_FOLDER_NAME).members();
			// all the corpus files must be in the traces subfolder
			for (int i=0; i<files.length; i++) {
				// assume that there are no sub-folders
				if (files[i].getType() != IResource.FILE) 
					continue;
				IFile file = (IFile) files[i];
				analyses.add(file);
			}
		} catch (CoreException e) {
			// tracefiles doesn't yet exist. no matter.
		}
	}

	private void addFile(CorpusFile cf) {
		switch (cf.getType()) {
		case CorpusFile.XML:
		case CorpusFile.FORESTXML:
		case CorpusFile.CSV:
		case CorpusFile.SEG:
		case CorpusFile.AUDIOSEG:
			xmlfiles.add(cf);
			break;
		case CorpusFile.VIDEO:
			videofiles.add(cf);
			break;
		default:
			System.err.println("unusual corpus file type: " + cf.getType());
		}
	}
	
	@Override
	public String toString() {
		return (project==null)?"":project.getName();
	}
	
	public Vector<CorpusFile> getXMLFiles() { // Don't modify the returned instance
		return new Vector<CorpusFile>(xmlfiles);
	}
	
	public Vector<String> getFilterableFilePaths() {
		Vector<String> ret = new Vector<String>();
		for (IFile file : replayables) {
			if (file.getName().startsWith(".")) {
				continue;
			}
			ret.add(file.getFullPath().makeRelative().toString());
		}
		for (IFile file : analyses) {
			if (file.getName().startsWith(".")) {
				continue;
			}
			ret.add(file.getFullPath().makeRelative().toString());
		}
		for (CorpusFile cf : xmlfiles) {
			ret.add(cf.getXmlFileWorkspacePath());
		}
		return ret;
	}
	
	public Vector<CorpusFile> getVideoFiles() {
		return new Vector<CorpusFile>(videofiles);
	}
	
	public Vector<CorpusFile> getFiles() {
		Vector<CorpusFile> ret = new Vector<CorpusFile>();
		ret.addAll(videofiles);
		ret.addAll(xmlfiles);
		return ret;
	}

	public IProject getProject() {
		return this.project;
	}

	public static boolean isAssociatedFile(String basefile, String candidate) {
		return candidate.startsWith(".") && candidate.substring(1).startsWith(basefile);
	}
	
	public static List<IResource> getAssociatedHiddenFiles(IFile file) {
		List<IResource> ret = new Vector<IResource>();
		String name = file.getName();
		try {
			IResource[] siblings = file.getParent().members();
			for (int i=0; i<siblings.length;i++) {
				if (siblings[i].getName().startsWith(".") &&
						siblings[i].getName().substring(1).startsWith(name)) {
					ret.add(siblings[i]);
				}
			}
		} catch (CoreException e) {
			e.printStackTrace();
		}
		return ret;
	}	
	

}
